﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Text.RegularExpressions;

namespace Toolkit.Web.Mvc.ExtensionMethods
{
	public static class MvcHtmlStringExtensions
    {
        /// <summary>
        /// Applies a title attribute to any and all option tags within the given string. It will automatically take
        /// the option text and insert it into a new option tag attribute.
        /// 
        /// Note: This assumes there is not already a title attribute applied to the option tags.
        /// </summary>
        /// <param name="mvcHtmlString"></param>
        /// <returns></returns>
        public static MvcHtmlString ApplyTitleAttributeToSelectListOptions(this MvcHtmlString value)
        {
            return MvcHtmlString.Create(
                Regex.Replace(value.ToHtmlString(), @"((?:<option)|(?:<option.*?['|""]))>(.*)(</option>)", @"$1 title=""$2"">$2$3")
            );
        }
	}
}
